<?php
/**
 * @version $Id$
 */
class BizbaseAction extends BaseAction {
	protected $biz;
	function _initialize() {
		parent::_initialize();
		self::exist_role(array(14,1));
		$this->biz=new Biz();
		
		//下单类型 1升级云商户2升级云购店
		$biz_stype = array(1=>'云购商',2=>'云购店');
		$this->assign('biz_stype',$biz_stype);
		//支付状态 0未付款 1线上付款失败 2线下付款失败 3付款中 4线上已付款 5线下已付款
		$biz_pay_status = array(0=>'未付款',1=>'线上付款失败',2=>'线下付款失败',3=>'付款中',4=>'线上已付款',5=>'线下已付款');
		$this->assign('biz_pay_status',$biz_pay_status);
		//处理状态 0未审核 1审核未通过 2审核通过
		$biz_status = array(0=>'未审核',1=>'审核未通过',2=>'审核通过');
		$this->assign('biz_status',$biz_status);
		//开通状态 0未开通 1关闭帐户 2开通帐户
		$biz_isclose = array(0=>'未开通',1=>'关闭帐户',2=>'开通帐户');
		$this->assign('biz_isclose',$biz_isclose);
		//操作类型：1申请 2付款 3审核 4状态 5修改
		$log_type = array(1=>'申请',2=>'付款  ',3=>'审核 ',4=>'状态',5=>'修改');
		$this->assign('log_type',$log_type);
		
		$area=F('Area');
        if(empty($area)){
            $area=M('Area')->select();
            $area=field2key($area,'id');
            F('Area',$area);
        }
		
        $this->assign('Area_list',$area);
		
		/*$this->assign('province',$this->_province);
		$this->assign('city',$this->_city);
		$this->assign('area',$this->_area);*/
	}
	/**
	 * 区域内所有套件
	 * */
    public function index(){
		//$condition=parent::create_where();
		$condition=array();
		
        if($this->keywords){
            $wherefields=explode(',',$_REQUEST['sfield_name']);
            if(in_array($_REQUEST['wherefields'], $wherefields)){
                $wherefields=$_REQUEST['wherefields'];
                $condition[$wherefields]=array("like",'%'.$this->keywords.'%');
                $this->assign('wherefields',$wherefields);
            }else{
            	$map=array();
                foreach ($wherefields as $value) {
                    $map[$value]=array("like",'%'.$this->keywords.'%');
                }
                $map['_logic'] = 'or';
                $condition['_complex'] = $map;
                $this->assign('wherefields','all');
            }
        }
		//商户类型条件查询
		$stype= intval($_REQUEST['stype']);
		if ($stype){
			$condition['stype'] = $stype;
		}
		//商户区域条件查询
		$province= intval($_REQUEST['province']);
		if ($province){
			$condition['province'] = $province;
		}
		$city= intval($_REQUEST['city']);
		if ($city){
			$condition['city'] = $city;
		}
		$area= intval($_REQUEST['area']);
		if ($area){
			$condition['area'] = $area;
		}
		//商户状态条件查询
		$pay_status = $_REQUEST[ 'pay_status' ];
         if ($pay_status != 'all' && !empty ($pay_status )){
               if ($pay_status == '-1' ){
                     $condition ['pay_status' ] = '0' ;
              } else {
                     $condition ['pay_status' ] = $pay_status;
              }
              
        }
         $status = $_REQUEST [ 'status'] ;
         if ($status != 'all' && !empty ($status )){
               if ($status == '-1' ){
                     $condition ['status' ] = '0' ;
              } else {
                     $condition ['status' ] = $status;
              }
        }
         $isclose = $_REQUEST [ 'isclose'] ;
         if ($isclose != 'all' && !empty ($isclose )){
               if ($isclose == '-1' ){
                     $condition ['isclose' ] = '0' ;
              } else {
                     $condition ['isclose' ] = $isclose;
              }
        }

		$this -> assign($_REQUEST);
		$order=$this->orderField.' '.$this->orderDirection;
		$totalRows = $this->biz->biz_list($condition,'all');
		$this->assign ( "totalCount", $totalRows );
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, $this->listrows );
			$pages=$page->show ();
			$list = $this->biz->biz_list($condition,$page->firstRow . ',' . $page->listRows ,$order);
			//dump($list);exit;
			$this->assign('list',$list);
		}
		$this->display();
    }
	public function edit(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据修改',0);
        }
		if ($_REQUEST ['dosubmit']) {
			$_info = $_POST['info'];
			$_info['starttime'] = strtotime($_POST['info']['starttime']);
			$_info['endtime'] = strtotime($_POST['info']['endtime']);
			$res=$this->biz->biz_edit($_info,array(),array(),$_info['user_id'],$this->userid);
			//修改商家平台-开通状态
			if(!empty($_info['isclose'])&&$_info['isclose']>0){
				$this->biz->biz_isclose($_info['id'],$_info['user_id'],$this->userid,$_info['isclose']);
			}
			$arr=object_array(json_decode($res));
			if($arr['status']==1){
				$this->_ajm(200,$arr['msg'],0,U ( 'Bizbase/index' ) );
			}else{
				$this->_ajm(300,$arr['msg'],0);
			}
		}
		$info = $this->biz->get_biz_detail($id);
		if(empty($info)){
            $this->_ajm(300,'查询数据出错或者查询的数据不存在',0);
        }
		$this->assign('info',$info);
		$this->display();
	}
	public function detail(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据查看',0);
        }
		$info = $this->biz->get_biz_detail($id);
		if(empty($info)){
            $this->_ajm(300,'查询数据出错或者查询的数据不存在',0);
        }
		$log = $this->biz->biz_log_list(array('userid'=>$info['user_id']));
		$this->assign('info',$info);
		$this->assign('log',$log);
		$this->display();
	}
	/**
	 * 添加云购商家
	 * */
	function cloudclientadd(){
		if ($_REQUEST ['dosubmit']) {
			//查询套件是否存在
			$kit = new Kit();
			$out_kit=array();//存储等待开通或其它操作的套件
			$info = $_POST['user_biz'];
			$info_post = $info;
			if(empty($info['user_id'])){
				$this->_ajm(300,'请点击空白处查询帐户是否存在!',0);
			}
			if(empty($info['stype'])){
				$this->_ajm(300,'请选择云购类型!',0);
			}
			$roleid = 0;
			if($info['stype']==1){
				$roleid = 15;
			}
			if($info['stype']==2){
				$roleid = 20;
			}
			$info['createtime'] = time();
			$info['starttime'] = $info['createtime'];
			$info['endtime'] = strtotime("+1 year",$info['createtime']);
			$info['pay_status'] = 0;
			$info['status'] = 0;
			$info['isclose'] = 0;
			$where=array();
			$where['kp.type']=$info['stype'];
			$where['starttime']=array('EQ', 0);
			$where['endtime']=array('EQ', 0);
			$where['province']=array('EQ', 0);
			$where['city']=array('EQ', 0);
			$where['area']=array('EQ', 0);
			$where['isclose']=array('EQ',0);
			/*switch ($this->_agent_stype) {
				case 1:
					$where['province']=$this->_province;
					$where['city']=array('EQ', 0);
					$where['area']=array('EQ', 0);
					$where['isclose']=array('EQ',0);
					break;
				case 2:
					$where['city']=$this->_city;
					$where['area']=array('EQ', 0);
					$where['isclose']=array('EQ',0);
					break;
				case 3:
					$where['area']=$this->_area;
					$where['isclose']=array('EQ',0);
					break;
				case 4:
					$where['isclose']=array('EQ',0);
					break;
			}*/
			$kit_list = $kit->kit_list($where,"0,1");
			if(empty($kit_list)){
				$this->_ajm(300,'可用套件不足，请先生成对应类型套件!',0);
			}
			$out_kit = $kit_list[0];
			if(empty($info['province'])||empty($info['city'])||empty($info['area'])||$info['province']==0||$info['city']==0||$info['area']==0){
				$this->_ajm(300,'区域不可为空!',0);
			}
			//判断当前区域商家是否具备开通条件
			/*$agent = new Agent();
			$agent_info = $agent->agent_list('all',array('vesting_province'=>$this->_province,'vesting_city'=>$this->_city,'vesting_area'=>$this->_area,'isclose'=>2));
			$biz_area = array('area'=>$info['area'],'city'=>$info['city'],'province'=>$info['province']);
			$agent_list = array();
			foreach ($biz_area as $key => $value) {
				foreach ($agent_info as $keys => $values) {
					if($value == $values['vesting_'.$key]){
						switch ($key) {
							case 'area':
								$agent_list[$value] = $values;
								break;
							case 'city':
								if($values['vesting_area']==0){
									$agent_list[$value] = $values;
								}
								break;
							case 'province':
								if($values['vesting_city']==0){
									$agent_list[$value] = $values;
								}
								break;
						}
					}
				}
			}
			if(count($agent_list)!=1){
				$this->_ajm(300,'您没权开通当前区域的云购!',0);
			}*/
			//添加商家平台信息
			$ret = $this->biz->biz_add($info,
										array(),
									  	array(
									  		'company' => '商家名称',
									  		'realname' => '负责人',
									  		'mobile' => '手机',
									  		'province' => '省份',
									  		'city' => '城市',
									  		'area' => '区域',
									  		'address' => '详细地址'
											),
										$info['user_id'],
										$this->userid,
										'云采购注册云购'
										);
			$arr=object_array(json_decode($ret));
			if($arr['status']==1){
				$info['pay_status'] = 0;
				$info['status'] = 0;
				$info['isclose'] = 0;
				//修改商家平台-付款状态
				if(!empty($info_post['pay_status'])&&$info_post['pay_status']>0){
					$this->biz->biz_pay_status($arr['id'],$info['user_id'],$this->userid,$info_post['pay_status']);
				}
				//修改商家平台-审核用户
				if(!empty($info_post['status'])&&$info_post['status']>0){
					$this->biz->biz_status($arr['id'],$info['user_id'],$this->userid,$info_post['status']);
				}
				//修改商家平台-开通状态
				if(!empty($info_post['isclose'])&&$info_post['isclose']>0&&$roleid>0){
					$this->biz->biz_isclose($arr['id'],$info['user_id'],$this->userid,$info_post['isclose'],$roleid);
				}
				if(!empty($info_post['isclose'])&&$info_post['isclose']==2&&$roleid>0){
					//操作套件归属
					$kit->kit_vesting(array(
											 'id' => $out_kit['id'],
											 'cloudsn' => $out_kit['cloudsn'],
											 'type' => $out_kit['kp_type']
											 ),array(),array(),$info['user_id'],$roleid,$this->userid);
					//修改套件时间
					$kit->kit_edit(array('id'=>$out_kit['id'],'starttime'=>$info['starttime'],'endtime'=>$info['endtime']),array(),array(),$this->userid,'系统生成合约时间');
					//修改套件状态
					$kit->kit_isclose(array('id'=>$out_kit['id'],'isclose'=>2),array(),array(),$this->userid,'开通套件');
				}
				$this->_ajm(200,$arr['msg'],0,U ( 'Bizbase/index' ) );
			}else{
				$this->_ajm(300,$arr['msg'],0);
			}
			exit ();
		}
		$this->display();
	}
	/**
	 * 修改云购商家开通资料
	 * */
	function cloudclientedit(){
		if ($_REQUEST ['dosubmit']) {
			dump($_POST);
			exit ();
		}
		$id = $_REQUEST['id'];
		$user=new User();
		$info = array();
		$info=$user->get_data("User_biz", array('user_id' => $id), "*", false);
		$username = $user->get_data("User", array('id' => $info['user_id']), "username", false);
		$info['username'] = $username['username'];
		$this->assign('user_biz',$info);
		
		$this->display('cloudclientadd');
	}
	/**
	 * 注册云采购帐号
	 * */
	function reg(){
		if ($_REQUEST ['dosubmit']) {
			//$ret=array();
			$arr=array();
			if(trim($_POST['password'])==trim($_POST['okpsd'])){
				$user=new User();
				$user->platform = '91YUNGOU';
				$userinfo = array();
				$uname = trim($_POST['username']);
				$userinfo['username']=trim($_POST['username']);
				$userinfo['password']=trim($_POST['password']);
				$userinfo['mobile']=trim($_POST['mobile']);
				$userinfo['email']=trim($_POST['email']);
				$res=$user->user_add($userinfo,array('username' => '用户名'),array('password' => '密码','username' => '用户名'),18,trim($_POST['id']));
				$arr=object_array(json_decode($res));
				//$ret=array();
				if($arr['status']==1){
					/*$ret['uname']=$uname;
					$ret['statusCode']=200;
					$ret['callbackType']='close';
					$ret['msg']=$arr['msg'];*/
					$this->_ajm(200,$arr['msg'],0);
				}else{
					/*$ret['statusCode']=300;
					$ret['msg']=$arr['msg'];*/
					$this->_ajm(300,$arr['msg'],0);
				}
			}else{
				/*$ret['statusCode']=300;
				$ret['msg']="密码确认有误！";*/
				$this->_ajm(300,$arr['msg'],0);
			}
			//exit (json_encode($ret));
		}
		$this->display();
	}
	/**
	 * 查询客户相关信息返回给客户查看校验用户帐户是否正确
	 * */
	public function ajax_users(){
		$res = array();
		$res['info']='';
		if(!empty($_POST['parent'])){
			$parent = $_POST['parent'];
			$user=new User();
			$info=$user->get_userinfo_all($parent, 'UserBiz');
			//user user_ext role_user platform_info
			if($info['user']){//帐户存在
				if($info['platform_info']){//帐户所在平台有信息
					$res['status'] = 2;//当前用户可以更新开通云购店
					$res['info'] = $info['platform_info'];
					$ok_count = 0;
					foreach ($info['role_user'] as $key => $value) {
						if($value['role_id'] == 20){
							$ok_count += 1;
						}elseif($value['role_id'] == 15){
							$ok_count += 1;
						}
					}
					if($ok_count>0){//用户已开通了云购店
						$res['status'] = 3;//当前用户已是云购店，不可重复开通
					}
				}else{//帐户所在平台没有信息
					$res['status'] = 1;//当前用户可以全新开通云购店
					$res['info']['user_id'] = $info['user']['id'];
					$res['info']['username'] = $info['user']['username'];
					$res['info']['email'] = $info['user']['email'];
					$res['info']['mobile'] = $info['user']['mobile'];
					$ok_count = 0;
					foreach ($info['role_user'] as $key => $value) {
						if($value['role_id'] == 20){
							$ok_count += 1;
						}elseif($value['role_id'] == 15){
							$ok_count += 1;
						}
					}
					if($ok_count>0){//用户已开通了云购店
						$res['status'] = 3;//当前用户已是云购店，不可重复开通
					}
				}
			}else{//帐户不存在
				$res['status'] = 0;//帐户不存在，请先注册云采购帐户
			}
		}	
		else {//系统有误
			$res['status'] = 4;//系统有错误
		}					
		exit(json_encode($res));
	}
}